package com.tailyou.androidlearning202400.act

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.CheckBox
import android.widget.CompoundButton
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import android.widget.Switch
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.tailyou.androidlearning202400.R
import com.tailyou.androidlearning202400.util.LogUtils
import com.tailyou.androidlearning202400.util.ToastUtil

class BaseWdgActivity : AppCompatActivity() {
    var rgSex: RadioGroup? = null
    var cbFootball: CheckBox? = null

    @SuppressLint("UseSwitchCompatOrMaterialCode")
    var switchAdult: Switch? = null
    var sbAge: SeekBar? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.enableEdgeToEdge()
        setContentView(R.layout.activity_base_wdg)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v: View, insets: WindowInsetsCompat ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        rgSex = findViewById(R.id.rgSex)
        rgSex?.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener { radioGroup, i ->
            val radioButton = findViewById<RadioButton>(i)
            // 往Logcat中打印日志信息
            //Log.d("性别：", radioButton.getText().toString());
            LogUtils.logd("性别：", radioButton.text.toString())
            // 通过Toast显示提示文字
            ToastUtil.showToast(this@BaseWdgActivity, radioButton.text.toString())
            // Toast.makeText(BaseWdgActivity.this, radioButton.getText().toString(), Toast.LENGTH_SHORT).show();
        })
        cbFootball = findViewById(R.id.cbFootball)
        cbFootball?.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener { compoundButton, b ->
            if (b) {
                //Log.d("足球：", "爱好里有足球");
                LogUtils.logd("爱好里有足球")
            } else {
                LogUtils.logd("兴趣里没有足球")
                //Log.d("足球：", "兴趣里没有足球");
            }
        })
        switchAdult = findViewById(R.id.swicthAdult)
        switchAdult?.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener { compoundButton, b ->
            if (b) {
                //Log.d("是否成年：", "已经成年");
                LogUtils.logd("是否成年：", "已经成年")
            } else {
                //Log.d("是否成年：", "尚未成年");
                LogUtils.logd("是否成年：", "尚未成年")
            }
        })
        sbAge = findViewById(R.id.sbAge)
        sbAge?.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, i: Int, b: Boolean) {
                if (i < 18) {
                    // 小于18岁，未成年
                    switchAdult?.setChecked(false)
                } else {
                    // 大于等于18岁，已成年
                    switchAdult?.setChecked(true)
                }
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {
                // 开始拖拽时触发该方法
            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
                // 结束拖拽时触发该方法
            }
        })
    }
}